from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from passlib.hash import argon2  # 使用 Argon2 

db = SQLAlchemy()

# ----------- 用户表 -----------
class User(db.Model):
    __tablename__ = 'user_db'
    user_id = db.Column(db.BigInteger, primary_key=True)
    user_name = db.Column(db.String(64), unique=True, nullable=False)
    password_hash = db.Column(db.String(255), nullable=False)  # 扩大字段长度
    phone_number = db.Column(db.String(32))
    membership_type = db.Column(db.Enum('member', 'premium'))
    subscription_end = db.Column(db.DateTime)
    create_at = db.Column(db.DateTime, default=datetime.now)
    
    def set_password(self, password):
        """使用 Argon2 生成密码哈希"""
        self.password_hash = argon2.hash(password)
    """使用 Argon2 验证密码"""
    def check_password(self, password):
    
        try:
            return argon2.verify(password, self.password_hash)
        except Exception as e:
            print(f"密码验证失败：{str(e)}")
            return False

# ----------- 收藏夹表 -----------
class UserCollect(db.Model):
    __tablename__ = 'usercollect_db'
    usercollect_id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.BigInteger, db.ForeignKey('user_db.user_id'), nullable=False)
    collect_name = db.Column(db.String(255))

# ----------- 收藏项表 -----------
class CollectItem(db.Model):
    __tablename__ = 'items_db'
    items_id = db.Column(db.BigInteger, primary_key=True)
    usercollect_id = db.Column(db.BigInteger, db.ForeignKey('usercollect_db.usercollect_id'), nullable=False)
    image_id = db.Column(db.String(64))  # 对应image_db的image_id

# ----------- 用户行为表 -----------
class UserAction(db.Model):
    __tablename__ = 'user_action_db'
    action_id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.BigInteger, db.ForeignKey('user_db.user_id'), nullable=False)
    action_type = db.Column(db.Enum('login', 'download', 'collect', 'comment'), nullable=False)
    image_id = db.Column(db.String(64))  # 可为空（例如登录操作不需要）
    comment_context = db.Column(db.Text)
    create_at = db.Column(db.DateTime, default=datetime.now)  # 确保datetime已导入

# ----------- 管理员表 -----------
class Admin(db.Model):
    __tablename__ = 'admin_db'
    admin_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    admin_name = db.Column(db.String(64))
    admin_password = db.Column(db.String(255))
    admin_phone_number = db.Column(db.String(255))
    admin_right = db.Column(db.Enum('super', 'normal'))

    # 在 models.py 的 Admin 类中添加
    def set_password(self, password):
        self.admin_password = argon2.hash(password)

    def check_password(self, password):
        try:
            return argon2.verify(password, self.admin_password)
        except Exception as e:
            print(f"密码验证失败：{str(e)}")
            return False

# ----------- 管理员操作表 -----------
class AdminAction(db.Model):
    __tablename__ = 'admin_action_db'
    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.String(64), db.ForeignKey('admin_db.admin_id'))
    admin_operation = db.Column(db.Enum('upload', 'delete', 'update', 'change_right'))
    operation_content = db.Column(db.Text)

# ----------- 图片表 -----------
class Image(db.Model):
    __tablename__ = 'image_db'

    # 核心元数据
    image_id = db.Column(db.BigInteger, primary_key=True)
    image_title = db.Column(db.String(255), nullable=False)
    image_description = db.Column(db.Text)
    # 存储信息
    resolution = db.Column(db.String(255))
    format = db.Column(db.String(255))
    midjourney_code = db.Column(db.JSON)
    codecopy_time= db.Column(db.BigInteger)
    beliked= db.Column(db.BigInteger)
    becollected= db.Column(db.BigInteger)
    comment_time = db.Column(db.Integer)
    comment_context = db.Column(db.Text)
    vip_exclusive = db.Column(db.String(255))

    # 分类标签
    image_tag1 = db.Column(db.String(255))
    image_tag2 = db.Column(db.String(255))
    image_tag3 = db.Column(db.String(255))



# ----------- 图片版本表 -----------
class ImageVersion(db.Model):
    __tablename__ = 'image_version_db'
    version_id = db.Column(db.BigInteger, primary_key=True)
    image_id = db.Column(db.String(64), db.ForeignKey('image_db.image_id'), nullable=False)
    admin_id = db.Column(db.String(64), db.ForeignKey('admin_db.admin_id'))
    audit_status = db.Column(db.String(255))
    oss_path = db.Column(db.String(255))
    upload_at = db.Column(db.DateTime)
    vip_exclusive = db.Column(db.Enum('yes', 'no', name='vip_exclusive_types'))


# ----------- VIP用户表 -----------
class UserVIP(db.Model):
    __tablename__ = 'user_vip_db'
    user_id = db.Column(db.BigInteger, db.ForeignKey('user_db.user_id'), primary_key=True)
    vip_status = db.Column(db.Enum('member', 'premium', name='vip_status_types'), default='member')
    start_date = db.Column(db.DateTime)
    end_date = db.Column(db.DateTime)
    renewal_count = db.Column(db.Integer, default=0)
    total_duration = db.Column(db.Integer, default=0)  # 单位：天
    last_renewal_date = db.Column(db.DateTime)

    user = db.relationship('User', backref=db.backref('vip_info', uselist=False))


#   VIP套餐表模型
class VIPPackage(db.Model):
    __tablename__ = 'vip_package'
    package_id = db.Column(db.Integer, primary_key=True)
    package_type = db.Column(db.String(50), unique=True, nullable=False)  # 如：monthly
    duration_days = db.Column(db.Integer, nullable=False)
    original_price = db.Column(db.Numeric(10, 2), nullable=False)
    discount_rate = db.Column(db.Numeric(3, 2), default=0)  # 0-1之间的小数
    is_discount_active = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)







